package com.zzh.lib.views.adapter;

import android.content.Context;

import com.zzh.lib.views.adapter.holder.HSuperViewHolder;
import com.zzh.lib.views.drag.OnDragItemTouchCallback;

import java.util.Collections;

/**
 * Created by ZZH on 2022/5/29.
 *
 * @Date: 2022/5/29
 * @Email: zzh_hz@126.com
 * @QQ: 1299234582
 * @Author: zzh
 * @Description: 可拖动的GridView
 */
public abstract class DragBaseAdapter<T> extends HListBaseAdapter<T> implements OnDragItemTouchCallback.ItemTouchAdapter {

    public DragBaseAdapter(Context ctx) {
        super(ctx);
    }

    private OnClickDragPreListener mOnItemDragClickListener;

    public OnClickDragPreListener getOnItemDragClickListener() {
        return mOnItemDragClickListener;
    }

    public void setOnItemDragClickListener(OnClickDragPreListener onItemDragClickListener) {
        mOnItemDragClickListener = onItemDragClickListener;
    }

    @Override
    public void onMove(int fromPosition, int toPosition) {
        if (fromPosition < toPosition) {
            for (int i = fromPosition; i < toPosition; i++) {
                Collections.swap(getDataList(), i, i + 1);
            }
        } else {
            for (int i = fromPosition; i > toPosition; i--) {
                Collections.swap(getDataList(), i, i - 1);
            }
        }
        notifyItemMoved(fromPosition, toPosition);
    }

    @Override
    public void onSwiped(int position) {
        getDataList().remove(position);
        notifyItemRemoved(position);
    }
    public interface OnClickDragPreListener {
        void onLongClickBook(HSuperViewHolder vh, int position);
    }
}
